package com.ctsims.web.controller.system;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ctsims.common.core.domain.entity.SysUser;
import com.ctsims.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ctsims.common.annotation.Log;
import com.ctsims.common.core.controller.BaseController;
import com.ctsims.common.core.domain.AjaxResult;
import com.ctsims.common.enums.BusinessType;
import com.ctsims.system.domain.CtsCourse;
import com.ctsims.system.service.ICtsCourseService;
import com.ctsims.common.utils.poi.ExcelUtil;
import com.ctsims.common.core.page.TableDataInfo;

/**
 * 课程Controller
 *
 * @author hhz
 * @date 2023-04-01
 */
@RestController
@RequestMapping("/system/course")
public class CtsCourseController extends BaseController
{
    @Autowired
    private ICtsCourseService ctsCourseService;

    @Autowired
    private ISysUserService userService;

    /**
     * 查询课程列表
     */
    @PreAuthorize("@ss.hasPermi('system:course:list')")
    @GetMapping("/list")
    public TableDataInfo list(CtsCourse ctsCourse)
    {
        startPage();
        List<CtsCourse> list = ctsCourseService.selectCtsCourseList(ctsCourse);
        return getDataTable(list);
    }

    /**
     * 导出课程列表
     */
    @PreAuthorize("@ss.hasPermi('system:course:export')")
    @Log(title = "课程", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CtsCourse ctsCourse)
    {
        List<CtsCourse> list = ctsCourseService.selectCtsCourseList(ctsCourse);
        ExcelUtil<CtsCourse> util = new ExcelUtil<CtsCourse>(CtsCourse.class);
        util.exportExcel(response, list, "课程数据");
    }

    /**
     * 获取课程详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:course:query')")
    @GetMapping(value = "/{courseId}")
    public AjaxResult getInfo(@PathVariable("courseId") Long courseId)
    {
        return success(ctsCourseService.selectCtsCourseByCourseId(courseId));
    }

    /**
     * 新增课程
     */
    @PreAuthorize("@ss.hasPermi('system:course:add')")
    @Log(title = "课程", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CtsCourse ctsCourse)
    {
        ctsCourse.setCreateBy(getNickname());
        ctsCourse.setCreateTime(new Date());
        return toAjax(ctsCourseService.insertCtsCourse(ctsCourse));
    }

    /**
     * 修改课程
     */
    @PreAuthorize("@ss.hasPermi('system:course:edit')")
    @Log(title = "课程", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CtsCourse ctsCourse)
    {
        return toAjax(ctsCourseService.updateCtsCourse(ctsCourse));
    }

    /**
     * 删除课程
     */
    @PreAuthorize("@ss.hasPermi('system:course:remove')")
    @Log(title = "课程", businessType = BusinessType.DELETE)
	@DeleteMapping("/{courseIds}")
    public AjaxResult remove(@PathVariable Long[] courseIds)
    {
        return toAjax(ctsCourseService.deleteCtsCourseByCourseIds(courseIds));
    }

    /**
     * 前端下拉列中展示可用的课程
     */
    @GetMapping("/selectCourseData")
    public AjaxResult selectCourseData()
    {
        CtsCourse ctsCourse= new CtsCourse();
        ctsCourse.setStatus("2"); //查询可用的课程
         List<CtsCourse> list = ctsCourseService.selectCtsCourseList(ctsCourse);
        return AjaxResult.success(list);
    }

    /**
     * 获取部门下的未删除学生姓名
     */
    @GetMapping("/selectAllStu")
    public AjaxResult selectAllStu()
    {
        List<SysUser> list = userService.selectAllStu();
        return AjaxResult.success(list);
    }
}
